package example.singleton;

/**
 * 防止反射暴力破坏单例
 */
public class Singleton02 {
    private static boolean isInstance = true;

    private Singleton02() {
        synchronized (Singleton02.class) {
            System.out.println("开始创建对象");
            if (isInstance) {
                isInstance = false;
            } else {
                throw new RuntimeException("请勿非法创建此对象");
            }
        }
    }

    public static Singleton02 getInstance() {
        return Holder.singleton02;
    }

    private static class Holder {
        private static Singleton02 singleton02 = new Singleton02();
    }
}
